/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BckFuzzyGUI.java
 *
 * Created on 09/08/2011, 09:45:40
 */

package ia.fuzzy.gui;

import ia.fuzzy.ConjuntoFuzzy;
import ia.fuzzy.SistemaFuzzy;
import java.awt.geom.Line2D;
import java.math.BigDecimal;

/**
 *
 * @author Gabriel
 */
public class FuzzyGUI extends javax.swing.JFrame {

    /** Creates new form BckFuzzyGUI */
    public FuzzyGUI() {
        
        initFuzzy();       
        initComponents();

        double temperaturaInicial = getTemperaturaMarcada();
        double volumeInicial = getVolumeMarcado();

        initInputs();
        fuzzy.executarMecanismoInferencia(temperaturaInicial, volumeInicial);
        initListeners();

        jPanelPressao.addGraficoResultante(new ConjuntoFuzzy(fuzzy.getVariaveisLinguisticas().get("Pressao"), fuzzy.getRegiaoNebulosaDeSaida()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanelTemperatura = new PainelDeGraficos(fuzzy.getVariaveisLinguisticas().get("Temperatura"), this);
        jLabel1 = new javax.swing.JLabel();
        jTextFieldTemperatura = new javax.swing.JTextField();
        jPanelVolume = new PainelDeGraficos(fuzzy.getVariaveisLinguisticas().get("Volume"), this);
        jLabel2 = new javax.swing.JLabel();
        jTextFieldVolume = new javax.swing.JTextField();
        jPanelPressao = new PainelDeGraficos(fuzzy.getVariaveisLinguisticas().get("Pressao"), this);
        jLabel3 = new javax.swing.JLabel();
        jTextFieldPressao = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanelTemperatura.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Temperatura", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 204))); // NOI18N

        jLabel1.setText("Temperatura:");

        javax.swing.GroupLayout jPanelTemperaturaLayout = new javax.swing.GroupLayout(jPanelTemperatura);
        jPanelTemperatura.setLayout(jPanelTemperaturaLayout);
        jPanelTemperaturaLayout.setHorizontalGroup(
            jPanelTemperaturaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTemperaturaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldTemperatura, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(84, Short.MAX_VALUE))
        );
        jPanelTemperaturaLayout.setVerticalGroup(
            jPanelTemperaturaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTemperaturaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelTemperaturaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldTemperatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(559, Short.MAX_VALUE))
        );

        jPanelVolume.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Volume", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 204))); // NOI18N

        jLabel2.setText("Volume:");

        javax.swing.GroupLayout jPanelVolumeLayout = new javax.swing.GroupLayout(jPanelVolume);
        jPanelVolume.setLayout(jPanelVolumeLayout);
        jPanelVolumeLayout.setHorizontalGroup(
            jPanelVolumeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelVolumeLayout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldVolume, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(88, Short.MAX_VALUE))
        );
        jPanelVolumeLayout.setVerticalGroup(
            jPanelVolumeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelVolumeLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelVolumeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldVolume, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(559, Short.MAX_VALUE))
        );

        jPanelPressao.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Pressão", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 204))); // NOI18N

        jLabel3.setText("Pressão:");

        javax.swing.GroupLayout jPanelPressaoLayout = new javax.swing.GroupLayout(jPanelPressao);
        jPanelPressao.setLayout(jPanelPressaoLayout);
        jPanelPressaoLayout.setHorizontalGroup(
            jPanelPressaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPressaoLayout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldPressao, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(78, Short.MAX_VALUE))
        );
        jPanelPressaoLayout.setVerticalGroup(
            jPanelPressaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPressaoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelPressaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldPressao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(559, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jPanelTemperatura, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jPanelVolume, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jPanelPressao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(21, 21, 21))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jPanelPressao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelVolume, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelTemperatura, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    public void initFuzzy()
    {
        fuzzy.criarVariavelLinguistica("Temperatura", 800, 1200);
        fuzzy.criarVariavelLinguistica("Volume", 2, 12);
        fuzzy.criarVariavelLinguistica("Pressao", 4, 12);
    }

    private void initListeners()
    {
        MouseHandler mouseHandler1 = new MouseHandler(jPanelTemperatura);
        jPanelTemperatura.addMouseListener(mouseHandler1);
        jPanelTemperatura.addMouseMotionListener(mouseHandler1);

        MouseHandler mouseHandler2 = new MouseHandler(jPanelVolume);
        jPanelVolume.addMouseListener(mouseHandler2);
        jPanelVolume.addMouseMotionListener(mouseHandler2);
    }

    public void initInputs()
    {
        jTextFieldTemperatura.setText(String.valueOf(getTemperaturaMarcada()));
        jTextFieldVolume.setText(String.valueOf(getVolumeMarcado()));
        jTextFieldPressao.setText(String.valueOf(getPressaoAtual()));

        jTextFieldTemperatura.addKeyListener(new TecladoHandler(this, "Temperatura"));
        jTextFieldVolume.addKeyListener(new TecladoHandler(this, "Volume"));
    }

    public void setTemperaturaInput()
    {
        Line2D linha = jPanelTemperatura.getLinha();
        double c = 965;

        Line2D novaLinha = new Line2D.Double(c, linha.getY1(), c, linha.getY2());
        jPanelTemperatura.setLinha(novaLinha);
        jPanelTemperatura.getGUI().executarInferencia();
        jPanelTemperatura.repaint();

    }

    public void setVolumeInput()
    {

    }

    public static double arredondar(double num, int casas) {
        int decimalPlace = casas;
        BigDecimal bd = new BigDecimal(num);
        bd = bd.setScale(decimalPlace,BigDecimal.ROUND_HALF_UP);
        num = bd.doubleValue();
        return num;
    }

    public void executarInferencia()
    {
        //fuzzy = new SistemaFuzzy();
        pressaoAtual = fuzzy.executarMecanismoInferencia(getTemperaturaMarcada(), getVolumeMarcado());
        jPanelPressao.atualizarGraficoResultante(fuzzy.getRegiaoNebulosaDeSaida());
        jPanelPressao.repaint();
    }

    private double getTemperaturaMarcada()
    {
        double x = jPanelTemperatura.getGraficos().get(0).getX();
        double lineX = jPanelTemperatura.getValorInicialLinha();
        double width = jPanelTemperatura.getGraficos().get(0).getWidth();

        double deltaTemp = (1200-800)/500; //igual a 0.8
        double deltaGraf = width/500;

        double temp = 800 + ((lineX-x)/deltaGraf)*0.8;
        return arredondar(temp, 2);
    }

    private double getVolumeMarcado()
    {
        double x = jPanelVolume.getGraficos().get(0).getX();
        double lineX = jPanelVolume.getValorInicialLinha();
        double width = jPanelVolume.getGraficos().get(0).getWidth();

        double deltaVol = (12-2)/500; //igual a 0.02
        double deltaGraf = width/500;

        double vol = 2 + ((lineX-x)/deltaGraf)*0.02;
        return arredondar(vol, 2);
    }

    private double getCoordenadaTemperatura()
    {
        double temp = Double.parseDouble(jTextFieldTemperatura.getText());
        //jTextFieldTemperatura.set
        double width = jPanelTemperatura.getGraficos().get(0).getWidth();
        double x0 = jPanelTemperatura.getGraficos().get(0).getX();

        double deltaTemp = (1200-800)/500; //igual a 0.8
        double deltaGraf = width/500;

        double x = x0 + (temp/0.8)*deltaGraf;

        System.out.println("VALOR DE X0: "+ x0);
        System.out.println("VALOR DE X: "+ x);
        System.out.println("VALOR DE X + Width: "+ (x+width));
        System.out.println("VALOR DE temp/0.8: "+ (temp/0.8));
        System.out.println("VALOR DE temp: "+ temp);
        return x;
    }

//    private double getCoordenadaVolume()
//    {
//
//    }

    private double getPressaoAtual()
    {
        return arredondar(pressaoAtual, 2);
    }
    public SistemaFuzzy getFuzzy()
    {
        return this.fuzzy;
    }


//    private class HandlerText implements ActionListener {
//
//        public void actionPerformed(ActionEvent e) {
//           if(e.getSource().equals(jTextFieldTemperatura))
//                  //jPanelTemperatura.setLinha(new Line2D(jPanelTemperatura.getLinha().getX1()));
//           else if(e.getSource().equals(jTextFieldVolume))
//               //do stuff
//        }  
//    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FuzzyGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private PainelDeGraficos jPanelPressao;
    private PainelDeGraficos jPanelTemperatura;
    private PainelDeGraficos jPanelVolume;
    private javax.swing.JTextField jTextFieldPressao;
    private javax.swing.JTextField jTextFieldTemperatura;
    private javax.swing.JTextField jTextFieldVolume;
    // End of variables declaration

    private SistemaFuzzy fuzzy = new SistemaFuzzy();
    private double pressaoAtual = 0;

}
